<template>
  <div id="app">
    <el-container>
      <el-aside width="200px" class="aside">
        <el-menu :default-active="activeMenu" class="el-menu-vertical-demo" @select="handleSelect">
          <el-menu-item index="1" @click="goTo('/user')">
            <i class="el-icon-user"></i>
            <p>用户页面</p>
          </el-menu-item>
          <el-menu-item index="2" @click="goTo('/order')">
            <i class="el-icon-menu"></i>
            <p>订单页面</p>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeMenu: this.getActiveMenu(),  // 刚开始默认是用户界面
    };
  },
  watch: {
    //路由器变化监听器
    $route(to, from) {
      this.activeMenu = this.getActiveMenu();
    }
  },
  methods: {
    getActiveMenu() {
      const path = this.$route.path;
      if (path === '/user') return '1'; // 用户管理页面
      if (path === '/order') return '2'; // 订单管理页面
      return '1';
    },
    handleSelect(index) {
      this.activeMenu = index;
    },
    goTo(path) {
      if (this.$route.path !== path) {
        this.$router.push(path);
      }
    }
  },
  created() {
    if (this.$route.path === '/') {
      this.goTo('/user');
    }
  }
};
</script>
